<?php

namespace app\api\controller\log;

use app\api\controller\common\ApiBase;
use app\api\model\AdminLog as AdminLogModel;
use app\api\model\Auth as AuthModel;

class AdminLog extends ApiBase
{

    public function index()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $where['id']=['>',0];
            $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
            if (isset($post['admin_name']) && $post['admin_name'] != '') {
                $where['admin_name'] = ['like' , '%' . $post['admin_name'] . '%'];
            }
            if (isset($post['start']) && $post['start'] != '') {
                $startTime=strtotime($post['start'].' 00:00:00');
                $endTime=strtotime($post['end'].' 23:59:59');
                $where['create_time'] = [
                    'between',
                    $startTime . ',' . $endTime
                ];
            }
            if (isset($post['admin_id']) && !empty($post['admin_id'])) {
                $where['admin_id'] = $post['admin_id'];
            }
            if (isset($post['rule']) && !empty($post['rule'])) {
                $rule=explode(',',$post['rule']);
                if(count($rule)==3){
                    $where['rule3'] = $post['rule'];
                }
                if(count($rule)==2){
                    $where['rule2'] = $post['rule'];
                }
                if(count($rule)==1){
                    $where['rule1'] = $post['rule'];
                }

            }
            $logList = AdminLogModel::where($where)->field('id,admin_name,content,ip,create_time,detail_content')->order('id desc')->paginate($pagesize);
            $logList = $logList->toArray();
            unset($logList['per_page'],$logList['last_page']);
            return json(array('code' => '1', 'msg' => '成功', 'data' => $logList));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 查看角色
     *
     * @return void
     */
    public function role()
    {
        if (request()->isPost()) {
            $authList = AuthModel::field('id,auth_name,status,rule,parent_id,sort,create_time,update_time')->select();
            $authList = collection($authList)->toArray();
            $authList = list_to_tree($authList);
            return json(array('code' => '1', 'msg' => '成功', 'data' =>$authList));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

}
